load("@fbcode_macros//build_defs:cpp_binary.bzl", "cpp_binary")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:custom_unittest.bzl", "custom_unittest")
load("//fboss/qsfp_service:qsfp_service.bzl", "QSFP_COMMON_COMPILER_FLAGS", "all_qsfp_core_libs", "all_qsfp_service_binaries")

oncall("fboss_optics_phy")

# Default qsfp service binary
# TODO(ccpowers): remove thisonce we support deploying a different qsfp service
# for each phy SDK version
cpp_binary(
    name = "qsfp_service",
    srcs = [
        "Main.cpp",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    versions = {
        "broadcom-plp-barchetta2": "5.2",
        "broadcom-plp-epdm": "4.1.2",
        "broadcom-plp-millenio": "5.5",
    },
    deps = [
        ":core-credo-0.7.2",
        ":handler",
        ":port-manager",
        ":stats",
        "//fboss/qsfp_service/platforms/wedge:wedge-platform-credo-0.7.2",
        "//folly/executors:function_scheduler",
        "//folly/logging:init",
        "//thrift/lib/cpp2:server",
    ],
)

cpp_library(
    name = "handler",
    srcs = [
        "QsfpServiceHandler.cpp",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        ":port-manager",
        ":transceiver-manager",
        "//common/fb303/cpp:fb303",
        "//fboss/agent:fboss-error",
        "//fboss/agent/hw:hardware_stats-cpp2-types",
        "//fboss/agent/if:ctrl-cpp2-types",
        "//fboss/fsdb/client:fsdb_pub_sub",
        "//fboss/fsdb/common:flags",
        "//fboss/lib:log_thrift_call",
        "//fboss/lib/phy:phy-cpp2-types",
        "//fboss/lib/phy:prbs-cpp2-types",
        "//fboss/mka_service/handlers:macsec_handler",
        "//fboss/qsfp_service/fsdb:fsdb-subscriber",
        "//fboss/qsfp_service/if:qsfp-cpp2-services",
        "//folly/futures:core",
        "//folly/logging:logging",
    ],
)

cpp_library(
    name = "core-default",
    exported_deps = [
        ":core-credo-0.7.2",
    ],
)

cpp_library(
    name = "stats-helper",
    srcs = [
        "facebook/StatsPublisherHelper.cpp",
    ],
    headers = [
        "facebook/StatsPublisherHelper.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        ":transceiver-manager",
        "//common/network:util",
        "//common/time:time",
        "//fb303:thread_cached_service_data",
        "//fboss/qsfp_service/if:transceiver-cpp2-types",
        "//folly/logging:logging",
    ],
)

cpp_library(
    name = "stats",
    srcs = [
        "facebook/StatsPublisher.cpp",
    ],
    headers = [
        "StatsPublisher.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        ":stats-helper",
        ":transceiver-manager",
        "//common/time:time",
        "//fb303:thread_cached_service_data",
        "//fboss/qsfp_service/if:transceiver-cpp2-types",
        "//folly/io/async:async_base",
        "//folly/logging:logging",
    ],
)

cpp_library(
    name = "transceiver-manager",
    srcs = [
        "TransceiverManager.cpp",
        "TransceiverStateMachine.cpp",
        "TransceiverStateMachineController.cpp",
    ],
    headers = [
        "StateMachineController.h",
        "TransceiverManager.h",
        "TransceiverStateMachine.h",
        "TypedStateMachineUpdate.h",
        "module/Transceiver.h",
        "module/TransceiverImpl.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        "fbsource//third-party/re2:re2",
        ":qsfp-config",
        ":slot-thread-helper",
        ":transceiver-validator",
        "//fb303:thread_cached_service_data",
        "//fboss/agent:fboss-error",
        "//fboss/agent:fboss-types",
        "//fboss/agent:platform_config-cpp2-types",
        "//fboss/agent:switch_config-cpp2-types",
        "//fboss/agent:utils",
        "//fboss/agent/if:ctrl-cpp2-types",
        "//fboss/agent/platforms/common:platform_mapping",
        "//fboss/fsdb/common:flags",
        "//fboss/lib:common_file_utils",
        "//fboss/lib:thread_heartbeat",
        "//fboss/lib/config:fboss_config_utils",
        "//fboss/lib/firmware_storage:firmware_storage",
        "//fboss/lib/i2c:i2c_controller_stats-cpp2-types",
        "//fboss/lib/phy:phy-cpp2-types",
        "//fboss/lib/phy:phy_management_base",
        "//fboss/lib/phy:prbs-cpp2-types",
        "//fboss/lib/restart_tracker:restart_time_tracker",
        "//fboss/lib/thrift_service_client:thrift-service-client",
        "//fboss/lib/usb:i2-api",
        "//fboss/lib/usb:transceiver_access_parameter",
        "//fboss/qsfp_service/fsdb:fsdb-syncer",
        "//fboss/qsfp_service/if:config-cpp2-types",
        "//fboss/qsfp_service/if:port_state-cpp2-types",
        "//fboss/qsfp_service/if:transceiver-cpp2-types",
        "//folly:file_util",
        "//folly:intrusive_list",
        "//folly:spin_lock",
        "//folly:string",
        "//folly:synchronized",
        "//folly/futures:core",
        "//folly/io/async:async_base",
        "//folly/json:dynamic",
        "//folly/logging:logging",
        "//thrift/lib/cpp/util:enum_utils",
    ],
    exported_external_deps = [
        "boost",
        ("boost", None, "boost_bimap"),
    ],
)

cpp_library(
    name = "qsfp-config",
    srcs = [
        "QsfpConfig.cpp",
    ],
    headers = [
        "QsfpConfig.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "//fboss/agent:fboss-error",
        "//fboss/qsfp_service/if:config-cpp2-types",
        "//folly:file_util",
        "//folly/logging:logging",
        "//thrift/lib/cpp2/protocol:protocol",
    ],
)

cpp_library(
    name = "transceiver-validator",
    srcs = [
        "TransceiverValidator.cpp",
    ],
    headers = [
        "TransceiverValidator.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "//fboss/lib:alert_logger",
        "//fboss/qsfp_service/if:transceiver_validation-cpp2-types",
        "//folly:string",
        "//folly/logging:logging",
        "//thrift/lib/cpp/util:enum_utils",
    ],
)

cpp_library(
    name = "typed-state-machine-update",
    headers = [
        "TypedStateMachineUpdate.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        "//folly/logging:logging",
    ],
)

cpp_library(
    name = "slot-thread-helper",
    srcs = [
        "SlotThreadHelper.cpp",
    ],
    headers = [
        "SlotThreadHelper.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "//fboss/agent:fboss-types",
        "//fboss/lib:thread_heartbeat",
        "//folly:intrusive_list",
        "//folly:spin_lock",
        "//folly:synchronized",
    ],
    exported_external_deps = [
        ("boost", None, "boost_bimap"),
    ],
)

cpp_library(
    name = "port-manager",
    srcs = [
        "PortManager.cpp",
        "PortStateMachine.cpp",
        "PortStateMachineController.cpp",
    ],
    headers = [
        "PortManager.h",
        "PortStateMachine.h",
        "StateMachineController.h",
        "TypedStateMachineUpdate.h",
    ],
    compiler_flags = QSFP_COMMON_COMPILER_FLAGS,
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        ":transceiver-manager",
        "//fboss/agent:fboss-error",
        "//fboss/agent:fboss-types",
        "//fboss/agent:switch_config-cpp2-types",
        "//fboss/agent:utils",
        "//fboss/agent/if:ctrl-cpp2-types",
        "//fboss/fsdb/common:flags",
        "//fboss/lib/config:fboss_config_utils",
        "//fboss/lib/thrift_service_client:thrift-service-client",
        "//fboss/qsfp_service/if:transceiver-cpp2-types",
        "//folly:spin_lock",
        "//folly:synchronized",
        "//folly/json:dynamic",
        "//folly/logging:logging",
        "//thrift/lib/cpp/util:enum_utils",
    ],
    exported_external_deps = [
        ("boost", None, "boost_bimap"),
        "boost",
    ],
)

custom_unittest(
    name = "test_qsfp_service_version",
    command = [
        "fboss/lib/test/test_version.sh",
        "$(exe :qsfp_service)",
    ],
    type = "simple",
)

all_qsfp_core_libs()

all_qsfp_service_binaries()
